<template>
    <div>
        <div class="userInfo">
            <div class="topBox">
                <div class="topTitle">
                    <img src="../../../assets/image/zxl/icon6.png" style="width: 18px;height: 18px;margin-right: 4px;">
                    <span>合同档案</span>
                </div>
            </div>
            <div style="margin-left: 28px;margin-top: 24px;">
                <el-table :data="tableData1" :height="tableData1.length == 0 ? 200 : ''" class="custom-table" style="width: 100%;">
                    <el-table-column prop="task_name" label="任务主题" align="center" />
                    <el-table-column label="合同类型" align="center">
                        <template #default="scope">
                            <div v-if="scope.row.isDrafts !== 1">
                                {{ gettype(scope.row.task.type) }}
                            </div>
                            <div v-else>
                                {{ gettype(scope.row.type) }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="CreateTime" label="时间" align="center">
                        <template #default="scope">
                            <div class="flex box-row-column">
                                <span v-if="scope.row.CreateTime" class="paddingsx03">开始时间:{{ scope.row.CreateTime
                                    }}</span>
                                <span v-if="scope.row.create_time" class="paddingsx03">开始时间:{{ scope.row.create_time
                                    }}</span>

                                <span v-if="scope.row.task.closing_date" class="paddingsx03">结束时间:{{
                                    scope.row.task.closing_date }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="参与人" align="center" v-if="shouldShowAgeColumn()">
                        <template #default="scope">
                            <el-popover placement="bottom" width="400" trigger="hover">
                                <!-- 自定义触发按钮 -->
                                <template #reference>
                                    <div class="flex box-row-column">
                                        <div class="flex box-row-column"
                                            :style="{ 'max-height': isExpanded ? 'none' : '60px', overflow: 'hidden' }">
                                            <div class="flex box-align-center paddingsx03"
                                                v-for="value in scope.row.join" :key="value">
                                                <el-icon size="16" color="#01509f"
                                                    v-if="!value.refusal_time && !value.sign_time"
                                                    style="margin-top: 2px; margin-right: 2px">
                                                    <clock />
                                                </el-icon>
                                                <el-icon size="16" color="#ff4345" v-if="value.refusal_time"
                                                    style="margin-top: 2px; margin-right: 2px">
                                                    <circle-close />
                                                </el-icon>
                                                <el-icon size="16" color="#2ca761" v-if="value.sign_time"
                                                    style="margin-top: 1px; margin-right: 2px">
                                                    <circle-check />
                                                </el-icon>
                                                {{ value.join_name }}
                                            </div>
                                        </div>
                                    </div>
                                </template>

                                <!-- 自定义 Popover 内容 -->
                                <div class="flex box-row-column popover-content">
                                    <div class="company-name flex box-row-column" v-for="value1 in scope.row.join"
                                        :key="value1">
                                        <div class="flex box-align-center">
                                            <el-icon size="16" color="#01509f"
                                                v-if="!value1.refusal_time && !value1.sign_time"
                                                style="margin-top: 1px; margin-right: 2px">
                                                <clock />
                                            </el-icon>
                                            <el-icon size="16" color="#ff4345" v-if="value1.refusal_time"
                                                style="margin-top: 1px; margin-right: 2px">
                                                <circle-close />
                                            </el-icon>
                                            <el-icon size="16" color="#2ca761" v-if="value1.sign_time"
                                                style="margin-top: 1px; margin-right: 2px">
                                                <circle-check />
                                            </el-icon>
                                            <div class="flex-1"> {{ value1.join_name }}</div>

                                            <div style="color: #01509f;" class="cursor" @click="gotocopy(value1)"
                                                v-if="value1.main_name == '个人'">
                                                签署链接
                                            </div>
                                        </div>

                                        <div class="mt06">
                                            {{ value1.join_phone }}
                                        </div>

                                        <img src="@/assets/enterprise/yirenzheng.png" style="width: 48px; height: 16px"
                                            class="mt06" />
                                        <!-- <img src="../assets/enterprise/weirenzheng.png" v-else> -->
                                    </div>
                                </div>
                            </el-popover>

                            <div class="flex box-align-center paddingsx03 cursor" v-if="scope.row.join.length > 2"
                                @click="isExpanded = !isExpanded">
                                <el-icon size="16" color="#01509f" style="margin-top: 2px; margin-right: 2px">
                                    <arrow-down v-if="!isExpanded" />
                                    <arrow-up v-else />
                                </el-icon>
                                <span style="font-size: 14px">{{ isExpanded ? "收起" : "展开" }}</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="证书" align="center" key="job_status" prop="job_status"
                        v-if="shouldShowAgeColumn()" width="150">
                        <template #default="scope">
                            <div class="flex box-align-center" v-if="!scope.row.reportUrl || scope.row.quKuaiUrl">
                                <img src="@/assets/image/cf/shu-1.png"
                                    style="width: 15px; height: 18px; margin-right: 5px" />
                                暂无证书
                            </div>
                            <div class="flex box-row-column box-align-start" v-else>
                                <div class="flex justify-content-center box-align-center cursor"
                                    @click="gotoUrl1(scope.row.reportUrl)" v-if="scope.row.reportUrl">
                                    <img src="@/assets/image/cf/shu-2.png"
                                        style="width: 15x; height: 18px; margin-right: 5px" />
                                    电子证书
                                </div>

                                <div class="flex justify-content-center box-align-center cursor" style="margin-top: 5px"
                                    @click="gotoUrl1(scope.row.quKuaiUrl)" v-if="scope.row.quKuaiUrl">
                                    <img src="@/assets/image/cf/qiyezhengshu.png"
                                        style="width: 15px; height: 18px; margin-right: 5px" />
                                    区块链证书
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center">
                        <template #default="scope">
                            <el-button style="font-size: 16px; color: #666666" text icon="View"
                                @click="gotoQianShu(scope.row, 3)">查看</el-button>
                            <el-button style="font-size: 16px; color: #ff4345" text icon="circle-close"
                                @click="gotojieyue(scope.row)" v-if="scope.row.is_break === 1">解约</el-button>
                            <el-button style="font-size: 16px; color: #3376f2" text icon="Collection"
                                @click="gotzhengshu(scope.row)">证书管理</el-button>
                        </template>
                    </el-table-column>
                    <template #empty>
                        <img src="../../..//assets/image/zxl/noData.png" width="94" style="width: 94px;height: 86px;" />
                    </template>
                </el-table>
                <Pagination style="justify-content: end;" :total="total1" :current-page="pageInfo1.page"
                    :page-size="pageInfo1.limit" @pagination-change="handlePaginationChange1" />
            </div>
        </div>
        <div class="userInfo" style="border-bottom: 0px solid #eaeaea;">
            <div class="topBox">
                <div class="topTitle">
                    <img src="../../../assets/image/zxl/icon7.png" style="width: 18px;height: 20px;margin-right: 4px;">
                    <span>操作记录</span>
                </div>
            </div>
            <div style="margin-left: 28px;margin-top: 24px;">
                <el-table :data="tableData2" :height="tableData2.length == 0 ? 200 : ''" class="custom-table" style="width: 100%;">
                    <el-table-column prop="content" label="操作内容" align="center">
                        <template #default="{ row }">
                            <span>{{ row.type + '：' + row.content }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="handle_name" label="操作人" align="center" />
                    <el-table-column prop="handle_time" label="操作时间" align="center" />
                    <template #empty>
                        <img src="../../..//assets/image/zxl/noData.png" width="94" style="width: 94px;height: 86px;" />
                    </template>
                </el-table>
                <Pagination style="justify-content: end;" :total="total2" :current-page="pageInfo2.page"
                    :page-size="pageInfo2.limit" @pagination-change="handlePaginationChange2" />
            </div>
        </div>

        <el-dialog v-model="zsVisible" width="30%" :before-close="handleClosezs" title="证书管理">
            <div class="flex flex-v mt20">
                <div class="flex justify-content-center box-align-center mb-6 cursor" @click="checkzstab(1)">
                    <div :class="zscheck === 1 ? 'flex box-align-center checkbj on1' : 'flex box-align-center checkbj'">
                        <div class="flex box-align-center px-4">
                            <img src="@/assets/image/cf/shu-2.png"
                                style="width: 20px; height: 20px; margin-right: 5px" />
                            <div class="dianzi_img">电子证书</div>
                        </div>
                        <div class="syzi">剩余次数：无限次</div>
                        <img src="@/assets/image/cf/yuanjiao2.png" alt="" class="jiaobiao" v-if="zscheck === 1" />
                    </div>
                    <div class="zi2 ml20 cursor" v-if="reportUrl" @click="gotoUrl(1)">查看证书</div>
                </div>
                <div class="flex justify-content-center box-align-center mb-6 cursor" @click="checkzstab(2)">
                    <div :class="zscheck === 2 ? 'flex box-align-center checkbj on1' : 'flex box-align-center checkbj'">
                        <div class="flex box-align-center px-4">
                            <img src="@/assets/image/cf/qiyezhengshu.png"
                                style="width: 20px; height: 20px; margin-right: 5px" />
                            <div class="dianzi_img">区块链证书</div>
                        </div>
                        <div class="syzi">剩余次数：无限次</div>
                        <img src="@/assets/image/cf/yuanjiao2.png" alt="" class="jiaobiao" v-if="zscheck === 2" />
                    </div>
                    <div class="zi2 ml20 cursor" v-if="quKuaiUrl" @click="gotoUrl(2)">查看证书</div>
                </div>
            </div>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" class="w-52" @click="goHeyanzs">确认申请</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from '@/TUIKit/adapter-vue';
import type { ElMessage, ElMessageBox, UploadProps } from "element-plus";
import { getCycleUserLog } from "@/api/xl";
import { manpowerlist, downloadChainReportApi } from "@/api/cf";
import { useRouter } from "vue-router";

const props = defineProps({
    user: {
        type: Object,
        default: () => ({}) // 对象/数组默认值需返回函数
    }
})

onMounted(() => {
    setTimeout(() => {
        getList()
        getList1()
    }, 1000)
});

const isExpanded = ref(false);
const zsVisible = ref(false);
const reportUrl = ref("");
const quKuaiUrl = ref("");
const zsid = ref(0);
const zscheck = ref(0);
const router = useRouter();
const pageInfo1 = ref({
    page: 1,
    limit: 5
})
const handlePaginationChange1 = ({ page, size }: { page: number; size: number }) => {
    console.log(page)
    console.log(size)
    pageInfo1.value.page = page
    pageInfo1.value.limit = size
    getList1()
}
const pageInfo2 = ref({
    page: 1,
    limit: 5
})
const getList1 = () => {
    manpowerlist({
        ...pageInfo1.value,
        operation: 5
    }).then(res => {
        console.log(res)
        tableData1.value = res.data;
        total1.value = res.count;
    });
}
const getList = () => {
    getCycleUserLog({
        ...pageInfo2.value,
        id: props.user.id
    }).then(res => {
        tableData2.value = res.data.data;
        total2.value = res.data.total;
    });
}
const handlePaginationChange2 = ({ page, size }: { page: number; size: number }) => {
    pageInfo2.value.page = page
    pageInfo2.value.limit = size
    getList()
}
const gotoUrl1 = a => {
    window.open(a, "_blank");
};
const shouldShowAgeColumn = () => {
    // 检查表格数据中是否有 showAge 为 true 的项
    return tableData1.value.some(item => item.isDrafts !== 1);
};
const gotoQianShu = (row, type) => {
    if (type === 3) {
        router.push({ name: "SigningCenterDetail", params: { id: row.task_id, type: type, userId: row.userId, usertype: row.join_type } });
    }
};
const gotojieyue = a => {
    ElMessageBox.confirm("是否确认发起解约流程?", "提示", {
        confirmButtonText: "去解约",
        cancelButtonText: "取消",
        type: "warning",
    }).then(() => {
        router.push({ name: "jieyue", params: { task_id: a.task_id, userid: a.userId, usertype: a.join_type } });
    });
    //dialogVisible.value = true;
};
const gotzhengshu = a => {
    zsVisible.value = true;
    zsid.value = a.Id;
    reportUrl.value = a.report_url;
    quKuaiUrl.value = a.qukuai_url;
};
const gotocopy = a => {
    const url = "https://demo-api.niuyuwang.com/sign";
    const input = document.createElement("input");
    input.value = url + "?tid=" + a.task_id + "&uid=" + a.userId;
    document.body.appendChild(input);
    input.select();
    document.execCommand("Copy");
    document.body.removeChild(input);
    ElMessage.success("复制成功");
};
const handleClosezs = () => {
    zsVisible.value = false;
};
const checkzstab = a => {
    zscheck.value = a;
};
const gotoUrl = a => {
    if (a === 1) {
        window.open(reportUrl.value, "_blank");
    } else {
        window.open(quKuaiUrl.value, "_blank");
    }
};
const goHeyanzs = () => {
    downloadChainReportApi(zsid.value, zscheck.value).then(res => {
        ElMessage.success("提交成功");
        zsVisible.value = false;
        handlePaginationChange1();
    });
};
const gettype = (row: any) => {
    switch (row) {
        case 1:
            return "入职合同";
        case 2:
            return "续签 ";
        case 3:
            return "规章制度";
        case 4:
            return "调岗";
        case 5:
            return "离职";
        case 6:
            return "自定义";
        case 8:
            return "解约";
    }
};

const tableData1 = ref([])
const tableData2 = ref([])
const total1 = ref(0)
const total2 = ref(0)
</script>

<style lang="scss">
.userInfo {
    padding: 12px 0 31px 0;
    border-bottom: 1px solid #eaeaea;

    .topBox {
        display: flex;
        align-items: center;

        .topTitle {
            display: flex;
            align-items: center;
            flex: 1;
            font-size: 16px;
            color: #01509f;
        }

        .topBtn {
            display: flex;
            align-items: center;

            .topBtnItem {
                cursor: pointer;
                ;
                width: 42px;
                height: 30px;
                color: #01509f;
                border: 1px solid #01509f;
                border-radius: 6px;
                text-align: center;
                line-height: 30px;
                font-size: 14px;
            }
        }
    }

    .custom-table .el-table__header th {
        color: #666;
        font-size: 16px;
        font-weight: lighter;
    }
}

.zi2 {
    font-size: 15px;
    color: #01509f;
    // font-family: 黑体;
}

.text-ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
    vertical-align: middle;
}

.company-name {
    padding: 10px 0;
    border-bottom: 1px solid #eaeaea;
}

.mb-6 {
    margin-bottom: 1.5rem !important;
}

.px-4 {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
}

.checkbj {
    border: solid 1px #eaeaea;
    background-color: #f5f5f5;
    width: 26.25rem !important;
    height: 2.625rem !important;
    position: relative;
    border-radius: 6px;
    font-size: 16px;
    justify-content: space-between;
}

.on1 {
    border-color: #01509f !important;
    background-color: #eaf5ff !important;
    color: #01509f !important;
}

.jiaobiao {
    width: 1.5rem;
    height: 1.5rem;
    position: absolute;
    right: 0px;
    top: 0px;
    z-index: 1;
}

.popover-content {
    max-height: 20rem !important;
    overflow-y: scroll;

    &::-webkit-scrollbar {
        display: none;
    }
}

.syzi {
    font-size: 13px;
    margin-right: 10px;
    color: #999;
}

.dianzi_img {}

.cell .el-button {
    margin-left: 0px !important;
    padding: 8px 10px !important;
}

.fonttrz {
    font-size: 16px;
    color: #333;
    font-weight: bold;
    margin-left: 5px;
}

.el-table {
    font-size: 16px;
    /* 默认大小 */
}

/* 设置表头文字 */
.el-table th {
    font-size: 18px;
}

/* 设置表格内容文字 */
.el-table td {
    font-size: 16px;
}

.table_anniu {
    height: 30px;
    border: solid 1px #eaeaea;
    border-radius: 6px;
    color: #666666;
    font-size: 15px;
    line-height: 30px;
    text-align: center;
    padding: 0 12px;
    cursor: pointer;
    margin: 0 10px;
}

.table_anniu:hover {
    border-color: rgb(198, 226, 255);
    background-color: rgb(236, 245, 255);
    color: #409eff;
}

.table_anniu.on {
    border-color: #01509f;
    background-color: #01509f;
    color: #fff;
}

.mainBox {
    width: 100%;
    height: 100%;
    overflow-y: scroll;
    // padding: 22px;
    // box-sizing: border-box;

    .topBox {
        background-color: #fff;
        border-radius: 12px;
        padding: 17px;
        box-sizing: border-box;

        .topTitle {
            font-size: 20px;
            font-weight: bolder;
            color: #333;
        }

        .topContent {
            display: flex;
            flex-direction: row;
            margin-top: 20px;
            gap: 30px;
            width: 100%;
            // padding: 17px 41px 0 41px;
            box-sizing: border-box;

            .topContentItem {
                display: flex;
                flex-direction: row;
                gap: 10px;
                align-items: center;
                border: solid 1px #e5e5e5;
                padding: 14px 10px;
                border-radius: 10px;
                box-sizing: border-box;
                flex-grow: 1;
                cursor: pointer;
                font-size: 15px;
            }

            .topContentItem:hover {
                background-color: #f7faff;
                border-color: #78aaff;
            }

            .zi3 {
                font-size: 16px;
            }

            .num_kuai {
                // width: 30%;
                margin-top: 10px;
                margin-right: 98px;
                text-align: center;
                cursor: pointer;
                transition: background-color 0.8s ease, transform 0.8s ease;
                will-change: background-color;
            }

            .checked {
                color: #fff;
                border-radius: 6px;
                padding: 6px 10px;
                width: 92px;
                height: 64px;
                box-sizing: border-box;

                .y1::before {
                    display: none;
                }

                .y2::before {
                    display: none;
                }

                .y3::before {
                    display: none;
                }

                .y4::before {
                    display: none;
                }

                .y5::before {
                    display: none;
                }

                .y6::before {
                    display: none;
                }
            }

            .zi_num {
                font-size: 24px;
                font-weight: bold;
            }

            .y1::before {
                content: "";
                display: inline-block;
                width: 10px;
                height: 10px;
                border-radius: 50%;
                background-color: #1864f2;
                margin-right: 5px;
            }

            .y2::before {
                content: "";
                display: inline-block;
                width: 10px;
                height: 10px;
                border-radius: 50%;
                background-color: #fe5c55;
                margin-right: 5px;
            }

            .y3::before {
                content: "";
                display: inline-block;
                width: 10px;
                height: 10px;
                border-radius: 50%;
                background-color: #2ad4ae;
                margin-right: 5px;
            }

            .y4::before {
                content: "";
                display: inline-block;
                width: 10px;
                height: 10px;
                border-radius: 50%;
                background-color: #ff7b43;
                margin-right: 5px;
            }

            .y5::before {
                content: "";
                display: inline-block;
                width: 10px;
                height: 10px;
                border-radius: 50%;
                background-color: #9188e5;
                margin-right: 5px;
            }

            .y6::before {
                content: "";
                display: inline-block;
                width: 10px;
                height: 10px;
                border-radius: 50%;
                background-color: #e5c388;
                margin-right: 5px;
            }
        }
    }

    .bottomBox {
        background-color: #fff;
        border-radius: 12px;
        padding: 17px;
        box-sizing: border-box;
        margin-top: 22px;

        .bottomTopBox {
            padding-bottom: 18px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #eaeaea;

            .bottomTitle {
                font-size: 20px;
                color: #333;
                font-weight: bolder;
                flex: 1;
            }

            .bottomRightBox {
                display: flex;
                align-items: center;

                .btnItem1 {
                    border-radius: 6px;
                    width: 108px;
                    height: 36px;
                    text-align: center;
                    line-height: 30px;
                    box-sizing: border-box;
                    font-size: 16px;
                    // color: #333;
                }

                .btnItem2 {
                    border: none;
                    border-radius: 6px;
                    width: 72px;
                    height: 36px;
                    text-align: center;
                    line-height: 30px;
                    box-sizing: border-box;
                    font-size: 16px;
                }
            }
        }
    }
}

.dialog-footer {
    display: flex;
    justify-content: center;
    /* 水平居中 */
    align-items: center;
    /* 垂直居中（可选） */
}

.w-52 {
    width: 13rem !important;
}

:deep(.el-table__header) {
    .table-header {
        th {
            font-size: 16px;
            background: #f3f8fb;
            height: 53px;
            color: #6f7070;
            font-weight: normal;
        }
    }
}
</style>
